2 using System.Collections.Generic;
5 using Microsoft.Xna.Framework;
6 using Microsoft.Xna.Framework.Content;
7 using Microsoft.Xna.Framework.Graphics;
9 namespace SuperPolarity
13 protected SuperPolarity game;
15 public List<Actor> Children;
18 protected Texture2D Texture;
19 protected Vector2 Origin;
22 protected Vector4 BoxDimensions;
23 protected Texture2D BoxTexture;
25 // Physical Properties
26 public Vector2 Position;
27 protected Vector2 Velocity;
28 protected Vector2 Acceleration;
31 // Constraints / Behavior
32 protected float MaxVelocity;
33 protected float AccelerationRate;
35 protected bool Immortal;
38 protected Color Color;
44 get { return Texture.Width; }
49 get { return Texture.Height; }
52 public Actor(SuperPolarity newGame)
57 public virtual void Initialize(Texture2D texture, Vector2 position)
63 Children = new List<Actor>();
65 Origin = new Vector2(Texture.Width / 2, Texture.Height / 2);
66 Velocity = new Vector2(0, 0);
67 Acceleration = new Vector2(0, 0);
70 AccelerationRate = 10;
83 BoxTexture = new Texture2D(game.GraphicsDevice, 1, 1);
84 BoxTexture.SetData(new Color[] { Color.White });
89 protected void InitBox()
91 Box = new Rectangle((int)(Position.X - BoxDimensions.X), (int)(Position.Y - BoxDimensions.X), (int)(BoxDimensions.X + BoxDimensions.W), (int)(BoxDimensions.Y + BoxDimensions.Z));
94 public void AutoDeccelerate(GameTime gameTime)
96 if (Acceleration.X == 0 && Velocity.X > 0)
98 if (AccelerationRate * gameTime.ElapsedGameTime.TotalSeconds > Velocity.X)
105 Acceleration.X = -AccelerationRate;
109 if (Acceleration.X == 0 && Velocity.X < 0)
111 if (-AccelerationRate * gameTime.ElapsedGameTime.TotalSeconds < Velocity.X)
118 Acceleration.X = AccelerationRate;
122 if (Acceleration.Y == 0 && Velocity.Y > 0)
124 if (AccelerationRate * gameTime.ElapsedGameTime.TotalSeconds > Velocity.Y)
131 Acceleration.Y = -AccelerationRate;
135 if (Acceleration.Y == 0 && Velocity.Y < 0)
137 if (-AccelerationRate * gameTime.ElapsedGameTime.TotalSeconds < Velocity.Y)
144 Acceleration.Y = AccelerationRate;
149 public virtual void Update(GameTime gameTime)
157 protected virtual void UpdateBox()
159 Box.X = (int)(Position.X - BoxDimensions.X);
160 Box.Y = (int)(Position.Y - BoxDimensions.Y);
163 public virtual void Move(GameTime gameTime)
165 AutoDeccelerate(gameTime);
167 var maxVelocity = MaxVelocity;
169 Velocity.X = Velocity.X + Acceleration.X * (float)gameTime.ElapsedGameTime.TotalSeconds;
170 Velocity.Y = Velocity.Y + Acceleration.Y * (float)gameTime.ElapsedGameTime.TotalSeconds;
172 if (Velocity.X > MaxVelocity)
174 Velocity.X = MaxVelocity;
177 if (Velocity.X < -MaxVelocity)
179 Velocity.X = -MaxVelocity;
182 if (Velocity.Y > MaxVelocity)
184 Velocity.Y = MaxVelocity;
187 if (Velocity.Y < -MaxVelocity)
189 Velocity.Y = -MaxVelocity;
192 Position.X = Position.X + Velocity.X;
193 Position.Y = Position.Y + Velocity.Y;
196 public void ChangeAngle()
198 Angle = (float)Math.Atan2(Velocity.Y, Velocity.X);
201 public virtual void Draw(SpriteBatch spriteBatch)
203 foreach (Actor child in Children)
205 child.Draw(spriteBatch);
208 spriteBatch.Draw(Texture, Position, null, Color, Angle, Origin, 1f, SpriteEffects.None, 0f);
209 spriteBatch.Draw(BoxTexture, Box, new Color(255, 0, 255, 25));
214 for (var i = Children.Count; i > 0; i--)
216 var actor = Children[i - 1];
217 if (actor.Position.X < -SuperPolarity.OutlierBounds || actor.Position.Y < -SuperPolarity.OutlierBounds ||
218 actor.Position.X > game.GraphicsDevice.Viewport.Width + SuperPolarity.OutlierBounds ||
219 actor.Position.Y > game.GraphicsDevice.Viewport.Height + SuperPolarity.OutlierBounds)
221 Children.Remove(actor);
226 public virtual void Collide(Actor other, Rectangle collision)
230 public void TakeDamage(int amount)
242 protected virtual void Die()